using System;
using System.Web;
using System.Web.Caching;

namespace SSP.Api
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class DirectorCache : DirectorWrapper
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DirectorCache"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public DirectorCache(Director parent) : base(parent)
        {
        }

        /// <summary>
        /// Instructs the DirectorPHP class to cache this request. The required key parameter is a unique string to cache the response with, and should be different for each page you set up. The expires parameter is optional and defaults to '+1 hour', meaning the cache will expire in one hour. Other examples include '+5 minutes', '+1 day', etc.
        /// In most cases, Director will be able to ping back to your API application and clear the cache. So, the expires parameter is more of a fallback than anything.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        public void Set(string cacheKey)
        {
            Parent.cache_key = cacheKey;
        }

        /// <summary>
        /// Instructs the DirectorPHP class to disable the cache for the next API call. The cache is automatically re-enabled for the following call.
        /// </summary>
        public void Disable()
        {
            Parent.Cache = null;
        }

        /// <summary>
        /// Gets the xml document specified by the tail from the cache.
        /// </summary>
        /// <param name="tail">The tail.</param>
        /// <returns></returns>
        public string Get(string tail)
        {
            var key = string.Format("{0}_{1}", Parent.cache_key, tail);
            if (HttpRuntime.Cache[key] != null)
            {
                return (string)HttpRuntime.Cache[key];
            }
            return null;
        }

        /// <summary>
        /// Fills the cache with the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="tail">The tail.</param>
        public void Fill(string data, string tail)
        {
            var key = string.Format("{0}_{1}", Parent.cache_key, tail);
            HttpRuntime.Cache.Add(key, data, null, System.Web.Caching.Cache.NoAbsoluteExpiration, Parent.cache_duration, CacheItemPriority.Default, null);
        }

        /// <summary>
        /// Instructs the DirectorPHP class to cache this request. The required key parameter is a unique string to cache the response with, and should be different for each page you set up. The expires parameter is optional and defaults to '+1 hour', meaning the cache will expire in one hour. Other examples include '+5 minutes', '+1 day', etc.
        /// In most cases, Director will be able to ping back to your API application and clear the cache. So, the expires parameter is more of a fallback than anything.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="span">The s1.</param>
        public void Set(string cacheKey, TimeSpan span)
        {
            Parent.cache_key = cacheKey;
            Parent.cache_duration = span;
        }
    }
}